package com.lixin.smartclassroom.ui.home.tab5.activity

import com.drake.brv.utils.bindingAdapter
import com.drake.channel.receiveEvent
import com.drake.channel.sendEvent
import com.drake.net.utils.scopeNetLife
import com.lixin.smartclassroom.api.getteacherpinglun
import com.lixin.smartclassroom.api.getwenjuandetail
import com.lixin.smartclassroom.api.paperdetail
import com.lixin.smartclassroom.api.pingjiabiaodan
import com.lixin.smartclassroom.api.submitpaper
import com.lixin.smartclassroom.api.submitpinglun
import com.lixin.smartclassroom.bean.DataListBean
import com.lixin.smartclassroom.bean.PaperItem
import com.lixin.smartclassroom.bean.TopicListDto
import com.lixin.smartclassroom.databinding.ActivityQuestionnaireDetailBinding
import com.lixin.smartclassroom.ui.home.adapter.questionnaireDetail
import com.lixinkeji.base.BaseActivity
import com.lixinkeji.extension.getIntentInt
import com.lixinkeji.extension.getIntentSerializable
import com.lixinkeji.extension.getIntentString
import com.lixinkeji.extension.gone
import com.lixinkeji.extension.onClick
import com.lixinkeji.extension.toast
import com.lixinkeji.extension.uId
import com.lixinkeji.extension.visible

/**
 * Description 问卷调查详情
 * Author: lwc
 * Date:2023/12/9 8:47
 */
class QuestionnaireDetailAct : BaseActivity<ActivityQuestionnaireDetailBinding>() {
    override fun getViewBinding() = ActivityQuestionnaireDetailBinding.inflate(layoutInflater)

    override fun topBarHide() = false
    override fun startPadding() = true
    private lateinit var listBean: ArrayList<DataListBean>
    private lateinit var topicList: ArrayList<TopicListDto>
    private var paperid = ""
    private var teacherid = ""
    private var paperBean: DataListBean? = null
    override fun title(): String {
        fromType = getIntentInt("type")
        teacherid = getIntentString("teacherid")
        when (fromType) {
            1 -> return "问卷调查"
            2, 3 -> return "评价老师"
        }
        return super.title()
    }

    private var fromType = 0


    override fun iniView() {
        topicList = ArrayList()
        listBean = ArrayList()
        if (fromType == 1) {
            paperBean = getIntentSerializable("paperBean")
            paperid = paperBean?.id.toString()
            binding.apply {
                llWenjuan.visible()
                tvSendTime.text = paperBean?.createdate
                if (paperBean?.state == "1") {
                    tvSubmitTime.gone()
                } else {
                    tvSubmitTime.visible()
                    tvSubmitTime.text = paperBean?.submittime
                }
            }
        } else if(fromType == 4){
            paperBean = getIntentSerializable("paperBean")
            paperid = paperBean?.id.toString()
            binding.apply {
                llWenjuan.visible()
                tvSendTime.text = paperBean?.createdate
                if (paperBean?.state == "1") {
                    tvSubmitTime.gone()
                } else {
                    tvSubmitTime.visible()
                    tvSubmitTime.text = paperBean?.submittime
                }
            }
        }

        binding.recycler.questionnaireDetail(fromType).models = listBean
        getData()

        receiveEvent<PaperItem>("itemAnswer") {
            listBean[it.parentPosition!!].topicanswer = it.answer
        }
        receiveEvent<TopicListDto>("zhuGuanTiAnswer") {
            listBean[it.parentPosition!!].topicanswer = it.answer
        }

    }

    private fun getData() {
        when (fromType) {
            1 -> {
                scopeNetLife {
                    val baseBean = paperdetail(paperid)
                    if (baseBean.result == "0") {
                        listBean.addAll(baseBean.dataList!!)
                        binding.recycler.bindingAdapter.notifyDataSetChanged()
                    } else {
                        toast(baseBean.resultNote.toString())
                    }
                }
            }

            2 -> {
                scopeNetLife {
                    val baseBean = pingjiabiaodan()
                    if (baseBean.result == "0") {
                        paperid = baseBean.id.toString()
                        listBean.addAll(baseBean.dataList!!)
                        binding.recycler.bindingAdapter.notifyDataSetChanged()
                    } else {
                        toast(baseBean.resultNote.toString())
                    }

                }
            }

            3 -> {
                binding.btnSave.gone()
                scopeNetLife {
                    val baseBean = getteacherpinglun(teacherid)
                    if (baseBean.result == "0") {
                        paperid = baseBean.id.toString()
                        listBean.addAll(baseBean.dataList!!)
                        binding.recycler.bindingAdapter.notifyDataSetChanged()
                    } else {
                        toast(baseBean.resultNote.toString())
                    }
                }
            }
            4->{
                binding.btnSave.gone()
                scopeNetLife {
                    val baseBean = getwenjuandetail(paperid)
                    if (baseBean.result == "0") {
                        paperid = baseBean.id.toString()
                        listBean.addAll(baseBean.dataList!!)
                        binding.recycler.bindingAdapter.notifyDataSetChanged()
                    } else {
                        toast(baseBean.resultNote.toString())
                    }
                }
            }
        }
    }

    override fun onClick() {

        binding.apply {
            btnSave.onClick {
                for (i in 0 until listBean.size) {
                    val topicDto = TopicListDto(listBean[i].topicId!!, listBean[i].topicanswer!!)
                    topicList.add(topicDto)
                }
                scopeNetLife {
                    if (fromType == 1) {
                        val baseBean = submitpaper(topicList, paperid, uId)
                        if (baseBean.result == "0") {
                            sendEvent("cs", "refreshWenJuanList")
                            finish()
                        }
                    } else {
                        val baseBean = submitpinglun(topicList, paperid, teacherid)
                        if (baseBean.result == "0") {
                            sendEvent("cs", "refreshWenJuanList")
                            finish()
                        }
                    }

                }
            }
        }
    }
}